<?php
namespace control;
/*
*转账单类
*/

class transferReceiptClass extends adIndexClass
{
    private const page = 'transferReceipt';
    private const table = 'transfer_order';
    private const title = '转账单';

    /**
     * 字段初始化
     */
    public function fieldInit($fun): array
    {
        switch ($fun) {
            default:
                $tr = [
                    'orderNo'          => [
                        'name'  => L('finance.table.dan_ju_bian_hao'),
                        'width' => 250,
                        'sort' => 1,
                    ],
                    'accountIn'  => [
                        'name'  => L('finance.table.zczh'),
                        'width' => 250,
                    ],
                    'accountOut'         => [
                        'name'  => L('finance.table.zrzh'),
                        'width' => 250,
                    ],
                    'money'          => [
                        'name'  => L('finance.table.jin_e') . '(' . static::$unit . ')',
                        'width' => 200,
                        'sort' => 1,
                    ],
                    'stid'          => [
                        'name'  => L('finance.table.zhi_dan_ren'),
                        'width' => 200,
                    ],
                    'recordDate'          => [
                        'name'  => L('finance.table.zzrq'),
                        'width' => 200,
                        'sort' => 1,
                    ],
                    'state'          => [
                        'name'  => L('finance.table.zhuang_tai'),
                        'width' => 200,
                    ]
                ];
        }
        return $tr;
    }

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    L('finance.table.dan_ju_bian_hao') => text(array("name" => "orderNo")),
                    L('finance.table.zrzh')   => idSelect(array('name' => 'acidIn', 'sqlForm' => "account where coid = '" . self::$coid . "'", 'sqlId' => 'id', 'sqlField' => 'name')),
                    L('finance.table.zczh') => idSelect(array('name' => 'acidOut', 'sqlForm' => "account where coid = '" . self::$coid . "'", 'sqlId' => 'id', 'sqlField' => 'name')),
                ],
                [
                    L('finance.table.zhuang_tai') => select(["name" => "state", 'option' => ['草稿', '待确认', '已确认']]),
                    L('finance.table.zhi_dan_ren') => selectMultiple(["name" => "stid", 'table' => " staff", 'sqlId' => 'stid', 'where' => "state !='离职'", 'sqlField' => 'name', 'class' => 'width200']),
                    L('finance.table.zzrq') => text(array("name" => "createTime", 'laydate' => ['type' => 'date', 'range' => '~'])),
                ]
            ]
        ],1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /*列表-查询条件*/
    public function home()
    {
        $html = $this->homeOptions(false);
        //控件
        $button = "";
        $url = root . "control/adIndex.html?cla=" . self::page;
        if ($this->power(self::page, "edit")) {
            $button .= "<span onclick=\"layer('".self::page."','edit')\" class='spanButton'>" . L('finance.table.add') . "</span>\n";
        }
        if ($this->power(self::page, "del")) {
            $button .= "<span class='spanButton' onclick=\"layer('{$this->get('cla')}', 'operate', {'act': 'del'})\">" . L('finance.table.shan_chu') . "</span>\n";
        }
        //返回
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /*客户列表-查询结果*/
    public function homeSearch()
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        //全等查询数组
        $equal = array(
            "acidIn" => $this->post('acidIn'),
            "acidOut" => $this->post('acidOut'),
            "stid" => $this->post('stid'),
            "state" => $this->post('state'),
            'coid' => self::$coid
        );
        $and = $this->seeSelf(self::page, 'stid');
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'time',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        //获取查询结果
        $where = search(array("like" => $like, "=" => $equal)) . $and;
        $page = $this->page(array("table" => self::table, "where" => $where, "order" => $orderBy['orderBy']));
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        $url = root . "control/adIndex.html?cla=" . self::page;
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
                'edit' => array("title" => L('finance.table.bian_ji'), "click" => "layer('" . self::page . "','edit',{id:'" . $array['id'] . "'})"),
                "detail" => array("title" => L('finance.table.transferReceipt'), "click" => "newIframe('" . L('finance.table.transferReceipt') . "','" . $url . "&fun=detail&id=" . $array['id'] . "')"),
            );
            if ($array['state'] == '已确认') {
                unset($dominate['edit']);
            }
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'accountIn':
                        $accountIn = query('account', "id = '$array[acidIn]'");
                        $row[] = $accountIn['name'];
                        break;
                    case 'accountOut':
                        $accountOut = query('account', "id = '$array[acidOut]'");
                        $row[] = $accountOut['name'];
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        //控件
        $button = "";
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
        ));
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='listForm'>" . $li . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }

    /**
     * 保存并提交
     * @return bool|string
     */
    public function saveAndCommit(): bool|string
    {
        $resultJson = $this->editEdit();
        $result = json_decode($resultJson,true);
        if ($result['warn'] == 2){
            unset($_SESSION['warn']);
            $resultJson = $this->commitEdit();
        }
        return $resultJson;
    }
    /**
     * 编辑页
     * @return string
     */
    public function edit()
    {
        $id = $this->post('id') ?: self::getId();
        $info = query(self::table, " id = '" . $id . "' ");
        //表单
        $html = $this->layerEdit([
            "table" => [
                [
                    must . L('finance.table.zczh') . "：" => idSelect(array('name' => 'acidOut', 'value' => $info['acidOut'], 'title' => L('finance.table.zczh'), 'sqlForm' => "account where coid = '" . self::$coid . "' and state = '启用'", 'sqlId' => 'id', 'sqlField' => 'name')),
                    must . L('finance.table.zrzh') . "：" => idSelect(array('name' => 'acidIn', 'value' => $info['acidIn'], 'title' => L('finance.table.zrzh'), 'sqlForm' => "account where coid = '" . self::$coid . "' and state = '启用'", 'sqlId' => 'id', 'sqlField' => 'name'))
                ],
                [
                    must . L('finance.table.jin_e') . "：" => text(["name" => "money", 'unit' =>  static::$unit , "value" => $info['money']]),
                    must . L('finance.table.jsfs') . "：" => select(["name" => "settleType", 'title' => L('finance.table.jsfs'), 'option' => explode('，', para('supplierSettlement')), "value" => $info['settleType']]),
                ],
                [
                    L('finance.table.jsdh') . "：" => text(["name" => "settleNo", 'class' => 'text', "value" => $info['settleNo']]),
                    must . L('finance.table.zzrq') . "：" => text(["name" => "recordDate", 'laydate' => [], "value" => $info['recordDate'] ?: $this->date]),
                ],
                [
                    L('finance.table.text') . "：" => textarea(["name" => "text", 'class' => 'textarea', "value" => $info['text']]),
                ],
            ],
            'hidden' => hidden(['name' => 'id', 'value' => $id]),
        ]);
        //返回
        return $this->layer(array("title" => L('job.button.editor').self::title, "height" => 350, "html" => $html));
    }

    public function editEdit()
    {
        $id = $this->post('id') ?: self::getId();
        $field = [
            'acidOut' => $this->post('acidOut'),
            'acidIn' => $this->post('acidIn'),
            'money' => $this->post('money'),
            'settleType' => $this->post('settleType'),
            'settleNo' => $this->post('settleNo'),
            'recordDate' => $this->post('recordDate'),
            'text' => $this->post('text'),
        ];
        $where = " id = '$id' ";
        $info = query(self::table, $where);
        //判断
        if (!$this->power(self::page, "edit")) {
            $json['warn'] = L('finance.table.trans_edit_permission');
        } elseif (!empty($info['id']) && $info['state'] == '已确认') {
            $json['warn'] = '此'.self::title.'已确认，不能修改';
        } else {
            $this->startTrans();
            if (!$info['id']) {
                $userNo = $orderNo = codeTemplateClass::generateCode(self::page, [
                    'prefix' => 'ZZD' . date('Ymd'),
                    'table' => self::table,
                    'field' => 'orderNo',
                    'where' => '',
                ], $field);
                $field = array_merge($field, [
                    'id' => $id,
                    'coid' => self::$coid,
                    'orderNo' => $userNo,
                    'state' => '草稿',
                    'stid' => $this->control['stid'],
                    'time' => $this->time
                ]);
                $json['warn'] = static::insert(["table" => self::table, "field" => $field]);
            } else {
                $json['warn'] = static::update([
                    "table" => self::table,
                    "field" => $field,
                    "where" => $where
                ]);
                $orderNo = $info['orderNo'];
            }
            $inAccountName = '';
            $OutAccountName = '';
            $bookkeepingIn = query('bookkeeping', "sourceId = '$id' and income >0");
            if($field['acidIn']) {
                $inAccount = query('account', "id = '$field[acidIn]'");
                $inAccountName = $inAccount['name'];
                if (empty($bookkeepingIn['id'])) {
                    //添加日记账
                    $fieldBookkeeping            = [
                        'id'          => suiji(),
                        'type'        => $inAccount['type'] == '现金' ? '现金' : '银行账户',
                        'coid'        => self::$coid,
                        'stid'        => self::$stid,
                        'accountId'   => $inAccount['id'],
                        'classify'    => '内部转账',
                        'recordDate'  => $field['recordDate'],
                        'summary'     => "转账单【{$orderNo}】",
                        'income'      => $field['money'],
                        'settleType'  => $field['settleType'],
                        'billNo'      => $field['settleNo'],
                        'text'        => $field['text'],
                        'sourceTable' => self::table,
                        'sourceId'    => $id
                    ];
                    $page                        = $inAccount['type'] == '现金' ? 'cashBook' : 'bankBook';
                    $userNo                      = codeTemplateClass::generateCode($page, [
                        'prefix' => date('Ymd'),
                        'table'  => 'bookkeeping',
                        'field'  => 'orderNo',
                        'where'  => '',
                    ], $fieldBookkeeping);
                    $fieldBookkeeping['orderNo'] = $userNo;
                    $bool                        = static::insert(["table" => 'bookkeeping', "field" => $fieldBookkeeping]);
                } else {
                    $bool = static::update(["table" => 'bookkeeping', "field" => [
                        'accountId'  => $inAccount['id'],
                        'classify'   => '内部转账',
                        'recordDate' => $field['recordDate'],
                        'summary'    => "转账单【{$orderNo}】",
                        'income'     => $field['money'],
                        'settleType' => $field['settleType'],
                        'billNo'     => $field['settleNo'],
                        'text'       => $field['text'],
                    ], 'where'                      => "id = '$bookkeepingIn[id]'"]);
                }
                if (!in_array($bool, ["新增成功", "更新成功"])) {
                    $json['warn'] = L('finance.table.scjzbsb');
                }
            }

            $bookkeepingOut = query('bookkeeping', "sourceId = '$id' and expenses >0");
            if($field['acidOut']) {
                $OutAccount = query('account', "id = '$field[acidOut]'");
                $OutAccountName = $OutAccount['name'];
                if (empty($bookkeepingOut['id'])) {
                    //添加日记账
                    $fieldBookkeeping            = [
                        'id'         => suiji(),
                        'type'       => $OutAccount['type'] == '现金' ? '现金' : '银行账户',
                        'coid'       => self::$coid,
                        'stid'       => self::$stid,
                        'accountId'  => $OutAccount['id'],
                        'classify'   => '内部转账',
                        'recordDate' => $field['recordDate'],
                        'summary'    => "转账单【{$orderNo}】",
                        'expenses'   => $field['money'],
                        'settleType' => $field['settleType'],
                        'billNo'     => $field['settleNo'],
                        'text'       => $field['text'],
                    ];
                    $page                        = $OutAccount['type'] == '现金' ? 'cashBook' : 'bankBook';
                    $userNo                      = codeTemplateClass::generateCode($page, [
                        'prefix' => date('Ymd'),
                        'table'  => 'bookkeeping',
                        'field'  => 'orderNo',
                        'where'  => '',
                    ], $fieldBookkeeping);
                    $fieldBookkeeping['orderNo'] = $userNo;
                    $bool                        = static::insert(["table" => 'bookkeeping', "field" => $fieldBookkeeping]);
                } else {
                    $bool = static::update(["table" => 'bookkeeping', "field" => [
                        'accountId'  => $OutAccount['id'],
                        'classify'   => '内部转账',
                        'recordDate' => $field['recordDate'],
                        'summary'    => "转账单【{$orderNo}】",
                        'expenses'   => $field['money'],
                        'settleType' => $field['settleType'],
                        'billNo'     => $field['settleNo'],
                        'text'       => $field['text'],
                    ], 'where'                      => "id = '$bookkeepingOut[id]'"]);
                }
                if (!in_array($bool, ["新增成功", "更新成功"])) {
                    $json['warn'] = L('finance.table.scjzbsb');
                }
            }

            if (in_array($json['warn'], ["新增成功", "更新成功"])) {
                if ($json['warn'] == '新增成功') {
                    self::insertOperateLog($id,self::title,"新建");
                }
                $this->commitTrans();
                //添加日志
                $text = str_replace(['{{staffName}}', '{{inAccount}}', '{{outAccount}}', '{{orderNo}}'], [$this->control['name'], $inAccountName, $OutAccountName, $orderNo], L('finance.table.trans_edit_text'));
                $this->logText(["type" => self::page, "text" => $text]);
                //返回
                $_SESSION['warn'] = $json['warn'];
                $json['warn'] = 2;
                $json['href'] = root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=detail&id=" . $id;
            } else {
                $this->rollback();
            }
        }

        resp:
        return json_encode($json, 320);
    }


    /*详情页*/
    public function detail(): string
    {
        $info = query(self::table, " id = '{$this->get('id')}'");
        if (empty($info['id'])) {
            $html = L('finance.table.wzdczzd');
        } else {
            //菜单
            $data = array(
                "id" => $info['id'],
            );

            $button = [];
            if ($this->power(self::page, "confirm") && $info['state'] == '待确认') {
                $button[] = [
                    "name" => '复核',
                    "type" => "弹出层",
                    "cla" => $this->get('cla'),
                    "fun" => "operate",
                    "post" => ["act" => 'confirm', "id" => $info['id']],
                    'style' => 'li-btn-blue-bg',
                ];
            }
            if ($info['state'] == '草稿') {
                if ($this->power(self::page, "edit")) {
                    //按钮
                    $button[] = [
                        "name" => L('finance.table.ti_jiao'),
                        "type" => "弹出层",
                        "cla" => $this->get('cla'),
                        "fun" => "operate",
                        "post" => ["act" => 'commit', "id" => $info['id']],
                        'style' => 'li-btn-blue-bg',
                    ];
                    //按钮
                    $button[] = [
                        "name" => L('finance.table.bian_ji'),
                        "type" => "弹出层",
                        "cla" => $this->get('cla'),
                        "fun" => "edit",
                        "post" => ["id" => $info['id']],
                    ];
                }
                if ($this->power(self::page, "del")) {
                    //按钮
                    $button[] = [
                        "name" => L('finance.table.shan_chu'),
                        "type" => "弹出层",
                        "cla" => $this->get('cla'),
                        "fun" => "operate",
                        "post" => ["act" => 'del', "id" => $info['id']],
                    ];
                }
            }
            //操作记录
            $button[] = self::operateLogBut($info['id'],1);

            $accountIn = query('account', "id = '$info[acidIn]'");
            $accountOut = query('account', "id = '$info[acidOut]'");
            //生成表单
            $data['button'] = $this->tableShowButton($button);
            $data['html'] = $this->tableShow(array(
                "基本信息" => array(
                    array(
                        L('finance.table.zczh') => $accountOut['name'],
                        L('finance.table.zrzh') => $accountIn['name'],
                        L('finance.table.jin_e') . "(" . static::$unit . ")" => $info['money'],
                        L('finance.table.jsfs') => $info['settleType'],
                    ),
                    array(
                        L('finance.table.jsdh') => $info['settleNo'],
                        L('finance.table.zzrq') => $info['recordDate'],
                    ),
                    array(
                        L('finance.table.bei_zhu') => pre($info['text']),
                    ),

                ),
                L('finance.table.xi_tong_xin_xi') => array(
                    array(
                        L('finance.table.dan_ju_bian_hao') => $info['orderNo'],
                        L('finance.table.zhi_dan_ren') => self::findStaff($info['stid']),
                        L('finance.table.zhuang_tai') => $info['state'],
                        L('finance.table.cjsj') => $info['time'],
                    ),
                    array(
                        L('finance.table.gxsj') => $info['updateTime'],
                    )
                )
            ));

            //生成页面
            $html = $this->tab($data);
        }
        //返回
        return $html;
    }
    /**
     * Desc : 提交前数据验证
     * User : you name
     * Date : 2023-11-17 16:04
     * @param $info
     * @return string|bool
     */
    public function dataValidation($info): string|bool
    {
        $annotation = [
            'acidOut' => L('finance.table.zczh'),
            'acidIn' => L('finance.table.zrzh'),
            'money' => L('finance.table.zzje'),
            'settleType' => L('finance.table.jsfs'),
            'recordDate' => L('finance.table.zzrq'),
        ];
        $rules = [
            'acidOut' => 'required',
            'acidIn' => 'required',
            'money' => 'required|numeric',
            'settleType' => 'required',
            'recordDate' => 'required',
        ];
        // 校验是否符合规则
        $vt = new \verify();
        $result = $vt->setAliases($annotation)->validate($info, $rules)->isOk();
        if ($result === false) {
            return $vt->getErrs()[0];  // 多个规则提示最前面一条
        }
        if ($info['acidIn'] == $info['acidOut']) {
            return L('finance.table.zrzhhzrzhbnxt');
        } elseif (accountClass::money($info['acidOut']) < $info['money']) {
            return L('finance.table.zczhdyebz');
        }

        return true;
    }
    /**
     * 提交
     */
    public function commitEdit()
    {
        $id = $this->post('id');
        $info = query(self::table, " id = '$id'");
        $field = [
            'state' => '待确认',
        ];
        //提交前数据验证
        $json['warn'] = $this->dataValidation($info);
        if($json['warn'] !== true){
            goto resp;
        }
        if (!$this->power(self::page, 'edit')) {
            $json['warn'] = L('finance.table.nhmzbjzx');
        } elseif (empty($info['id'])) {
            $json['warn'] = L('finance.table.wzdczzd');
        } elseif ($info['state'] != '草稿') {
            $json['warn'] = L('finance.table.czzdbscgztbntj');
        } else {
            $this->startTrans();
            $json['warn'] = update(array(
                "table" => self::table,
                "field" => $field,
                "where" => "id = '$info[id]'",
            ));
            if (in_array($json['warn'], ["新增成功", "更新成功"])) {
                // 单据是否需要复核
                if(!in_array(self::page,self::$publicSettings['documentReview'])){
                    $resultJson = $this->confirmEdit();
                    $json = json_decode($resultJson,true);
                    if ($json['warn'] != 2){
                        $this->rollback();
                        goto resp;
                    }
                }else {
                    self::insertOperateLog($info['id'],self::title,"提交");
                }
                $this->commitTrans();
                //添加日志
                $text = str_replace(['{{staffName}}', '{{orderNo}}', '{{id}}'], [$this->control['name'], $info['orderNo'], $id], L('finance.table.trans_commit_text'));
                $this->logText(["type" => self::page, "text" => $text]);
                //返回
                $_SESSION['warn'] = $json['warn'];
                $json['warn'] = 2;
                $json['href'] = root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=detail&id=" . $info['id'];
            } else {
                $this->rollback();
            }
        }
        resp:
        return json_encode($json, 320);
    }

    /**
     * 确认
     */
    public function confirmEdit()
    {
        $id = $this->post('id');
        $info = query(self::table, " id = '$id'");
        $field = [
            'state' => '已确认',
            'checkId' => self::$stid,
            'confirmTime' => $this->time
        ];
        if (!$this->power(self::page, 'confirm')) {
            $json['warn'] = L('finance.table.nhmzqrzx');
        } elseif (empty($info['id'])) {
            $json['warn'] = L('finance.table.wzdczzd');
        } elseif ($info['state'] != '待确认') {
            $json['warn'] = L('finance.table.czzdbsdqrztbnqr');
        } elseif (accountClass::money($info['acidOut']) < $info['money']) {
            $json['warn'] = L('finance.table.zczhdyebz');
        } else {
            $this->startTrans();
            $json['warn'] = update(array(
                "table" => self::table,
                "field" => $field,
                "where" => "id = '$info[id]'",
            ));
            $bool = accountClass::record_ac($info['acidOut'], '支出', $info['money'], "转账单【{$info['orderNo']}】支出", L('finance.table.zhhz'), hiddenText: "转账单【{$info['orderNo']}】支出，ID:{$info['id']}");
            if ($bool !== true) {
                $json['warn'] = $bool;
            }
            $bool = accountClass::record_ac($info['acidIn'], '收入', $info['money'], "转账单【{$info['orderNo']}】收入", L('finance.table.zhhz'), hiddenText: "转账单【{$info['orderNo']}】收入，ID:{$info['id']}");
            if ($bool !== true) {
                $json['warn'] = $bool;
            }
            if (in_array($json['warn'], ["更新成功"])) {
                // 单据是否需要复核
                if(!in_array(self::page,self::$publicSettings['documentReview'])) {
                    self::insertOperateLog($info['id'],self::title,"提交、复核");
                }else{
                    self::insertOperateLog($info['id'],self::title,"复核");
                }
                $this->commitTrans();
                //添加日志
                $text = str_replace(['{{staffName}}', '{{orderNo}}', '{{id}}'], [$this->control['name'], $info['orderNo'], $id], L('finance.table.trans_confirm_text'));
                $this->logText(["type" => self::page, "text" => $text]);
                //返回
                $_SESSION['warn'] = $json['warn'];
                $json['warn'] = 2;
                $json['href'] = root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=detail&id=" . $info['id'];
            } else {
                $this->rollback();
            }
        }
        resp:
        return json_encode($json, 320);
    }

    // 操作中转
    public function operate()
    {
        $act = $this->post('act') ?? '';
        $pwd = $word = false; // 需要密码
        switch ($act) {
            case 'confirm':
                $act = 'confirm';
                $msg = L('finance.table.que_ren');
                $title = L('finance.table.sfyqrczzdqrhjbkxg');
                $function = 'confirmEdit';
                $form = self::page . 'OperateForm, detailForm';
                $power = '';
                $word = true;
                break;
            case 'commit':
                $act = 'edit';
                $msg = L('finance.table.bian_ji');
                $title = L('finance.table.sfqrtjczzd');
                $function = 'commitEdit';
                $form = self::page . 'OperateForm, detailForm';
                $power = '';
                $word = true;
                break;
            default:
                $act = 'del';
                $msg = L('finance.table.transDel');
                $title = self::page . 'Delete';
                $function = 'delete';
                $form = self::page . 'OperateForm, listForm';
                $power = '';
                break;
        }
        if (!$this->power(self::page, $act)) {
            $_SESSION['warn'] = str_replace(['{{msg}}'], [$msg], L('jon.other.permission_warn'));
            $json = [
                'warn' => 2,
                'href' => root . 'control/adIndex.html?cla=' . $this->get('cla') . '&fun=home',
            ];
        } else {
            return $this->confirm([
                'title' => $title,
                'function' => $function,
                'form' => $form,
                'power' => $power,
                'pwd' => $pwd,
                'word' => $word
            ]);
        }

        resp:
        return json_encode($json, 320);
    }

    /**
     * 删除
     * @return bool|string
     */
    public function delete()
    {
        $array = $this->post('listId') ?? $this->post('id');
        if (!$this->power(self::page, "del")) {
            $json['warn'] = L('finance.table.nmzscdzx');
        } elseif (empty($array)) {
            $json['warn'] = L('finance.table.nygsjdmzxzn');
        } else {
            $text = "";
            $x = 0;
            $array = is_array($array) ? $array : [$array];
            foreach ($array as $id) {
                $info = query(self::table, " id = '$id' ");
                //最后删除基本参数
                delete(array("table" => self::table, "where" => " id = '$id' "));
                //返回
                $warn = L('finance.table.sccg');
                $x++;
                $text .= str_replace(['{{orderNo}}', '{{id}}'], [$info['orderNo'], $info['id']], L('finance.table.trans_del_text')) . $warn . "。<br>";

            }
            //添加日志

            $text = str_replace(['{{staffName}}'], [$this->control['name']], L('finance.table.trans_del_log')) . "<br>" . $text;
            $this->logText(array("type" => self::page, "text" => $text));
            $_SESSION['warn'] = str_replace(['{{x}}'], [$x], L('finance.table.trans_del_msg'));
            $json['warn'] = 2;
            $json['href'] = root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=home";
        }
        return json_encode($json, 320);
    }
}

